/**
 * 车次核销管理初始化
 */
var TTrain = {
    id: "TTrainTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TTrain.initColumn = function () {
    return [
        {type: 'checkbox', fixed: 'left'},
        {title: '车次号', field: 'train_no',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '上游网点', field: 'send_dept_name',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '车牌号', field: 'car_no',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '司机', field: 'driver_name', width: 100,visible: true, align: 'center', valign: 'middle'},
        {title: '入库时间', field: 'report_date',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '票数', field: 'ps',width: 100, visible: true, align: 'center', valign: 'middle'},
        {title: '应收', field: 'ys', visible: true, width: 100,align: 'center', valign: 'middle'},
        {title: '应付', field: 'yf', visible: true, width: 100,align: 'center', valign: 'middle'}

    ];
};

/**
 * 检查是否选中
 */

TTrain.check = function () {

    var selected = mTable.checkStatus(TTrain.id);
    if (selected.data.length == 0||selected.data.length>1) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TTrain.seItem = selected.data[0];
        return true;
    }
};

/**
 * 点击添加车次核销
 */
TTrain.openAddTTrain = function () {
    var index = layer.open({
        type: 2,
        title: '车次核销',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/tTrain/tTrain_add'
    });
    layer.full(index);
    this.layerIndex = index;
};

/**
 * 打开查看车次核销详情
 */
TTrain.openTTrainDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '核销详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/tTrain/tTrain_update/' + TTrain.seItem.id
        });
        layer.full(index);
        this.layerIndex = index;
    }
};

/**
 * 删除车次核销
 */
TTrain.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/tTrain/delete", function (data) {
            Feng.success("删除成功!");
            TTrain.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("tTrainId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询车次核销列表
 */
TTrain.search = function () {
    var mstatus='';
    if($("#status").val()==""){
        mstatus="0";
    }else{
        mstatus=$("#status").val()+"";
    }
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['status'] =mstatus ;
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    aTable.refresh(queryData);
};

var aTable = null
var mTable = null;
$(function () {
    $('#status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"核销状态"
    });

    var defaultColunms = TTrain.initColumn();
    aTable = new LYTable(TTrain.id, "/tTrain/list", defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        TTrain.table = aTable.init(mTable);
    })


});

$(document).keyup(function(event) {
    if (event.keyCode == 13) {
        TTrain.search();
    }
});
